package com.googlecode.etrade.client.acaotab;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.store.GroupingStore;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.grid.AggregationRenderer;
import com.extjs.gxt.ui.client.widget.grid.AggregationRowConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.grid.GridGroupRenderer;
import com.extjs.gxt.ui.client.widget.grid.GroupColumnData;
import com.extjs.gxt.ui.client.widget.grid.GroupingView;
import com.extjs.gxt.ui.client.widget.grid.SummaryType;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.extjs.gxt.ui.client.widget.table.NumberCellRenderer;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.Element;
import com.googlecode.etrade.client.comum.Papel;

public class GridAcoes extends LayoutContainer {

	@Override
	protected void onRender(Element parent, int index) {
		super.onRender(parent, index);
		setLayout(new FlowLayout());

		final NumberFormat number = NumberFormat.getFormat("0.00");
		final NumberCellRenderer<Grid<Papel>> numberRenderer = new NumberCellRenderer<Grid<Papel>>(
				number);

		GridCellRenderer<Papel> change = new GridCellRenderer<Papel>() {
			public String render(Papel model, String property,
					ColumnData config, int rowIndex, int colIndex,
					ListStore<Papel> store, Grid<Papel> grid) {
				double val = (Double) model.get(property);
				String style = val < 0 ? "red" : "green";
				return "<span style='color:" + style + "'>"
						+ number.format(val) + "</span>";
			}
		};

		GridCellRenderer<Papel> gridNumber = new GridCellRenderer<Papel>() {
			public String render(Papel model, String property,
					ColumnData config, int rowIndex, int colIndex,
					ListStore<Papel> store, Grid<Papel> grid) {
				return numberRenderer.render(null, property, model
						.get(property));
			}
		};

		GroupingStore<Papel> store = new GroupingStore<Papel>();
		store.add(getPapeis());
		store.groupBy("nome");

		List<ColumnConfig> config = new ArrayList<ColumnConfig>();

		ColumnConfig compania = new ColumnConfig("nome", "Companhia", 60);
		config.add(compania);

		ColumnConfig cod = new ColumnConfig("cod", "Codigo", 20);
		config.add(cod);

		ColumnConfig qtdAcoes = new ColumnConfig("qtdAcoes", "Qtd. Ações", 20);
		qtdAcoes.setAlignment(HorizontalAlignment.RIGHT);
		config.add(qtdAcoes);

		ColumnConfig preco = new ColumnConfig("valCompra",
				"Preço de Compra (R$)", 20);
		preco.setRenderer(gridNumber);
		preco.setAlignment(HorizontalAlignment.RIGHT);
		config.add(preco);

		ColumnConfig cotacao = new ColumnConfig("cotacao", "Cotação (R$)", 20);
		cotacao.setAlignment(HorizontalAlignment.RIGHT);
		config.add(cotacao);

		ColumnConfig variacao = new ColumnConfig("variacao", "Variação (%)", 20);
		variacao.setAlignment(HorizontalAlignment.RIGHT);
		variacao.setRenderer(change);
		config.add(variacao);

		// ColumnConfig last = new ColumnConfig("data", "Última atualização",
		// 20);
		// last.setDateTimeFormat(DateTimeFormat.getFormat("dd/MM/y"));
		// config.add(last);

		AggregationRowConfig<Papel> totalizador = new AggregationRowConfig<Papel>();
		totalizador.setHtml("name", "Total");
		totalizador.setSummaryType("cotacao", SummaryType.SUM);
		totalizador.setSummaryFormat("cotacao", number);
		totalizador.setRenderer("cotacao", new AggregationRenderer<Papel>() {
			public Object render(Number value, int colIndex, Grid<Papel> grid,
					ListStore<Papel> store) {
				// you can return html here
				return number.format(value.doubleValue());
			}
		});

		final ColumnModel cm = new ColumnModel(config);

		cm.addAggregationRow(totalizador);
		
		GroupingView view = new GroupingView();
		view.setShowGroupedColumn(false);
		view.setForceFit(true);
		view.setGroupRenderer(new GridGroupRenderer() {
			public String render(GroupColumnData data) {
				String f = cm.getColumnById(data.field).getHeader();
				String l = data.models.size() == 1 ? "Item" : "Items";
				return f + ": " + data.group + " (" + data.models.size() + " "
						+ l + ")";
			}
		});

		Grid<Papel> grid = new Grid<Papel>(store, cm);
		grid.setView(view);
		grid.setBorders(true);

		LayoutContainer panel = new LayoutContainer();
		panel.setLayout(new FitLayout());
		panel.setHeight(400);
		panel.add(grid);

		add(panel);
	}

	private List<Papel> getPapeis() {
		ArrayList<Papel> pList = new ArrayList<Papel>();
		Papel papel = new Papel("Abyara", "ABYA3", 200, 4.60, new Date(), 4.61,
				"Imovel");
		Papel papel3 = new Papel("Abyara", "ABYA3", 100, 4.70, new Date(),
				4.61, "Imovel");
		Papel papel2 = new Papel("Cyrela", "CYRE3", 300, 4.60, new Date(),
				4.61, "Imovel");
		pList.add(papel);
		pList.add(papel2);
		pList.add(papel3);
		return pList;
	}
}
